package com.example.registration.filter;

import com.example.registration.utils.Constants;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * ��¼������
 * @author Administrator
 *
 */
//@WebFilter(filterName = "test", urlPatterns = "/view/*")登录拦截
@WebFilter(filterName = "test", urlPatterns = "/view/*")
public class LoginFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		Object user = request.getSession().getAttribute(Constants.USER_LOGIN_KEY);
		if(user != null){//
			chain.doFilter(servletRequest, servletResponse);
		}else{//δ��¼
			HttpServletResponse response = (HttpServletResponse)servletResponse;
			String contextPath = request.getSession().getServletContext().getContextPath();
			response.sendRedirect(contextPath+"/login.html");
		}
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}
	
}
